package com.qsmx.storeapp;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.display.DisplayManager;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.fustion.FustionPre;
import com.qsmx.store.app.fustion.FustionTag;
import com.qsmx.store.app.main.login.LoginDelegate;
import com.qsmx.store.app.main.login.LoginMachineDelegate;
import com.qsmx.store.app.util.CoinOutUtil;
import com.qsmx.store.app.util.DisplayUtil;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.activities.ProxyActivity;
import com.qsmx.store.core.app.Atmos;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.event.QrScanEvent;
import com.qsmx.store.core.fusion.FusionCode;
import com.qsmx.store.core.fusion.FusionString;
import com.qsmx.store.core.util.log.AtmosLogger;
import com.qsmx.store.core.util.storage.AtmosPreference;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Iterator;

import qiu.niorgai.StatusBarCompat;

public class MainActivity extends ProxyActivity {

    private final StringBuilder sb = new StringBuilder();
    private static final int END_CHAR_ID = 66;
    private static final int BLANK_CHAR_ID = 59;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Atmos.getConfigurator().withActivity(this);
        StatusBarCompat.translucentStatusBar(this, true);
        DisplayUtil.hideBottomNav(this);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        FustionManager.coinOutUtil.InitUsb(this);

//        util.SendCmd(3, 10);
    }


    @Override
    public AtmosDelegate setRootDelegate() {
        return new LoginMachineDelegate();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        showShortToast("扫码成功");
        if (FusionCode.SCAN_TYPE.equals(FusionCode.QR_SCANNER)) {
            showShortToast(FusionCode.QR_SCANNER);
            System.out.println("keycode=========>" + keyCode + "");
            char keyValue = (char) event.getUnicodeChar();
            // 生成二维码有大写字母的时候，会有空格出现，这个就是过滤空格的
            if (keyCode != END_CHAR_ID && keyCode != BLANK_CHAR_ID) {
                sb.append(keyValue);
            } else if (!TextUtils.isEmpty(sb.toString()) && keyCode != BLANK_CHAR_ID) {

                if (!FusionCode.QrIsLoading) {
                    FusionCode.QrIsLoading = true;
                    String qrCode = sb.toString().trim().replaceAll("\\s*", "");

                    Bundle bundle = new Bundle();
                    bundle.putString(FusionString.QR_CODE, qrCode);
                    EventBus.getDefault().removeAllStickyEvents();
                    EventBus.getDefault().postSticky(new QrScanEvent(bundle));

                    sb.setLength(0);
                }
            }

            // 结束符去掉，不然有的界面不需要扫描功能，导致它扫描都内容叠加
            if (keyCode == END_CHAR_ID) {
                sb.setLength(0);
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
